<?php
C_base::Dy('c.admin.adbase');
class xitonggl extends adbase
{
	var $_vpath='admin/xitong/';  //定义调用模板所属一级文件夹
	function __construct(){
		parent::__construct();
		$this->titl = '网站管理';
		$this->xurl = '/admin/?act=xitong'.$this->gnpd;
	}
	
	function index(){
		$this->tit = '网站设置';
		$r = C_base::M('admin.xitonggl')->config_s();
		$this->r = $r;
		/*
		 * 域名
		 * 网站名称
		 * 允许上传的图片类型
		 * 允许上传的图片最大值
		 * 允许上传的文件类型
		 * 允许上传的文件最大值
		 */
	}
	
	function con_u(){
		$this->_is_html = 1;
		extract($this->_post());
		if(empty($url)){
			exit('-1');//域名不能为空
		}
		if(empty($title)){
			exit('-2');//网站标题不能为空
		}
		$a = C_base::M('admin.xitonggl')->config_s();
		$w = array('title'=>$a['title']);
		$u = array('url'=>$url,'title'=>$title,'imgtype'=>$imgtype,'imgsize'=>$imgsize,'filetype'=>$filetype,'filesize'=>$filesize);
		$r = C_base::M('admin.xitonggl')->config_u($w,$u);
		if($r == 1){
			exit('cg');
		}else{
			exit('error');
		}
		
	}
	
	function guanli(){
		$this->tit = '管理员管理';
		$w = array('id!'=>0);
		$this->r = C_base::M('admin.xitonggl')->admin_ss($w);
		/*
		 * 组
		 * 上级管理员(当前登录管理员的名字)
		 * 账号
		 * 密码
		 * 重复密码
		 * 是否有创建管理员的权限
		 * 性别
		 * 是否被锁定
		 * 昵称
		 * 是否只允许一个人登录
		 * 
		 * username	用户名
		 * password	密码
		 * sjid		上级管理员的id(0代表是超管)
		 * sid		上级管理员的id的集合
		 * sd		是否锁定
		 * sex		性别
		 * name		昵称
		 */
	}
	
	//修改管理员状态
	function admin_zt(){
		extract($this->_post());
		if($id != $_SESSION['admin_id']){
			$w = array('id'=>$_SESSION['admin_id']);
			$r = C_base::M('admin.xitonggl')->admin_s($w);
			$w1 = array('id'=>$id);
			$r1 = C_base::M('admin.xitonggl')->admin_s($w1);
			if($r['sjid'] != 0 && $r1['sjid'] != $r['id']){
				exit('-1');//无权限
			}
			$u = array('sd'=>$sd);
			$r2 = C_base::M('admin.xitonggl')->admin_u($w1,$u);
			if($r2 == 1){
				exit('cg');
			}else{
				exit('error');
			}
		}else{
			exit('-2');//自己不能修改自己的状态
		}
	}

	//添加管理员-页面
	function add_admin(){
		$this->tit = '管理员-添加';
		$this->zu = C_base::M('admin.xitonggl')->zu_ss(array());
	}
	
	//添加管理员
	function admin_tj(){
		$this->_is_html = 1;
		extract($this->_post());
		if(empty($zh)){
			exit('-1');//账号不能为空
		}
		if(empty($pwd)){
			exit('-2');//密码不能为空
		}
		if(empty($pwd1)){
			exit('-3');//确认密码不能为空
		}
		if(empty($name)){
			exit('-4');//昵称不能为空
		}
		if($pwd != $pwd1){
			exit('-5');//两次密码不一样
		}
		if(empty($zu)){
			exit('-7');//组不能为空
		}
		$w = array('username'=>$zh);
		$r1 = C_base::M('admin.xitonggl')->admin_s($w);
		if($r1){
			exit('-6');//该账号已被注册
		}
		$c = array('username'=>$zh,'password'=>md5($pwd),'name'=>$name,'sjid'=>$_SESSION['admin_id'],'sex'=>$sex,'sd'=>$sd,'zu'=>$zu,'time'=>time());
		$r = C_base::M('admin.xitonggl')->admin_c($c);
		if($r > 0){
			$w1 = array('id'=>$_SESSION['admin_id']);
			$r2 = C_base::M('admin.xitonggl')->admin_s($w1);			
			$u = array('sid'=>$r2['sid'].$r.'|');
			$w2 = array('id'=>$r);
			$r3 = C_base::M('admin.xitonggl')->admin_u($w2,$u);
			if($r3 == '1'){
				exit('cg');
			}else{
				C_base::M('admin.xitonggl')->admin_d($w2);
				exit('error');
			}
			exit('cg');
		}else{
			exit('error');
		}
	}
	
	//管理员修改-页面
	function bj_admin(){
		extract($this->_get());
		$this->tit = '管理员-修改';
		$w = array('id'=>$id);
		$this->r = C_base::M('admin.xitonggl')->admin_s($w);
		$this->zu = C_base::M('admin.xitonggl')->zu_ss(array());
	}
	
	function admin_xg(){
		$this->_is_html = 1;
		extract($this->_post());
		if(empty($name)){
			exit('-1');//昵称不能为空
		}
		if(empty($pwd)){
			exit('-2');//密码不能为空
		}
		if(empty($pwd1)){
			exit('-3');//确认密码不能为空
		}
		if($pwd != $pwd1){
			exit('-4');//两次密码不一样
		}
		if(empty($zu)){
			exit('-5');//组不能为空
		}
		$u = array('name'=>$name,'sex'=>$sex,'sd'=>$sd,'zu'=>$zu);
		$w = array('id'=>$id);
		$r = C_base::M('admin.xitonggl')->admin_s($w);
		if($r['password'] != $pwd){
			$u['password'] = md5($pwd);
		}
		$rr = C_base::M('admin.xitonggl')->admin_u($w,$u);
		if($rr == '1'){
			exit('cg');
		}else{
			exit('error');
		}
	}
	
	function admin_sc(){
		extract($this->_post());
		$w = array('id'=>$_SESSION['admin_id']);
		$r = C_base::M('admin.xitonggl')->admin_s($w);
		$w1 = array('id'=>$id);
		$r1 = C_base::M('admin.xitonggl')->admin_c($w1);
		if($r['sjid'] == 0 || $r1['sjid'] == $_SESSION['admin_id']){
			$r2 = C_base::M('admin.xitonggl')->admin_d($w1);
			if($r2 == '1'){
				exit('cg');
			}else{
				exit('error');
			}
		}else{
			exit('-1');//无权限
		}
	}
	
	function zu(){
		$this->tit = '组管理';
		$w = array();
		$this->r = C_base::M('admin.xitonggl')->zu_ss($w);
		/*
		 * 页面展示两项
		 * 组名称
		 * 组权限
		 * 
		 * 表字段
		 * name
		 * qx
		 * cy成员
		 * 
		 */
	}
	
	function add_zu(){
		$this->tit = '组-添加';
		$this->wzgn = C_base::M('admin.gongnengl')->gn_ss(array('sjid'=>0));  //获取功能模块
	}
	
	function zu_tj(){
		$this->_is_html = 1;
		extract($this->_post());
		if(empty($name)){
			exit('-1');//组名称不能为空
		}
		if(empty($qx)){
			exit('-2');//权限不能为空
		}
		$qx = ','.implode(',',$qx).',';
		$c = array('name'=>$name,'qx'=>$qx,'time'=>time());
		$r = C_base::M('admin.xitonggl')->zu_c($c);
		if($r > 0){
			exit('cg');
		}else{
			exit('error');
		}
	}
	
	function bj_zu(){
		$this->tit = '组-修改';
		extract($this->_get());
		$w = array('id'=>$id);
		$this->r = C_base::M('admin.xitonggl')->zu_s($w);
		$this->qx = json_encode(explode(',',$this->r['qx']));
		$this->wzgn = C_base::M('admin.gongnengl')->gn_ss(array('sjid'=>0));  //获取功能模块
	}
	
	function zu_xg(){
		$this->_is_html = 1;
		extract($this->_post());
		if(empty($name)){
			exit('-1');//组名称不能为空
		}
		if(empty($qx)){
			exit('-2');//权限不能为空
		}
		$w = array('id'=>$id);
		$qx = ','.implode(',',$qx).',';
		$u = array('name'=>$name,'qx'=>$qx);
		$r = C_base::M('admin.xitonggl')->zu_u($w,$u);
		if($r == '1'){
			exit('cg');
		}else{
			exit('error');
		}
	}
	
	function zu_sc(){
		$this->_is_html = 1;
		extract($this->_post());
		$w = array('id'=>$id);
		$r = C_base::M('admin.xitonggl')->zu_d($w);
		if($r == '1'){
			exit('cg');
		}else{
			exit('error');
		}
	}
	
	function tcsz(){
		$this->tit = '套餐设置';
		$this->r = C_base::M('admin.xitonggl')->tc_ss(array());
	}
	
	function tcsz_sck(){
		$this->_is_html = 1;
		extract($this->_post());
		if(@$name){
			$bb = C_base::M('admin.xitonggl')->tc_s($this->_post());
			if($bb){
				exit('套餐名称不能重复');
			}
			$na = mb_ereg_replace('^(　| )+','', @$name);//去除全角字符的空格
			$nam = mb_ereg_replace('(　| )+$','', $na);//去除全角字符的空格
			if(trim($nam)){
				$cd=(strlen(trim($nam)) + mb_strlen(trim($nam),'UTF8'))/2;
				if ($cd >30) {
					exit('套餐名称不能超过30字符');
				}
			}else{
				exit('套餐名称不能为空');
			}
		}		
		if(@$kfb){
			if(@$kfb < 0 || @!preg_match('/^[0-9]*$/', $kfb)){
				exit('可发布数量必须为不为负数的整数');
			}
		}		
		if(@$kxz){
			if(@$kxz < 0 || @!preg_match('/^[0-9]*$/', $kxz)){
				exit('可下载数量必须为不为负数的整数');
			}
		}
		if(@$money){
			if(!is_numeric($money)){
				exit('请正确填写');
			}	
		}
	    $rs = C_base::M('admin.xitonggl')->tc_u(array('id'=>$id),$this->_post());
	    if($rs){
	    	exit('cg');
	    }else{
	    	exit('error');
	    }
		
	}
}